/*
实现一个LazyMan，可以按照以下方式调用:
LazyMan(“Hank”)输出:
Hi! This is Hank!

LazyMan(“Hank”).sleep(10).eat(“dinner”)输出
Hi! This is Hank!
//等待10秒..
Wake up after 10
Eat dinner~

LazyMan(“Hank”).eat(“dinner”).eat(“supper”)输出
Hi This is Hank!
Eat dinner~
Eat supper~

LazyMan(“Hank”).sleepFirst(5).eat(“supper”)输出
//等待5秒
Wake up after 5
Hi This is Hank!
Eat supper
以此类推。
 */
class LazyMan {
    constructor(name) {
        this.q = []
        this.name = name
        this.say(this.name)
        setTimeout(() => {
            this.run()
        },0)
    }
    say() {
        this.q.push(() => console.log("This is", this.name))
        return this
    }
    sleep(time) {
        this.q.push(() => {
            const start = +new Date();
            while (start + time * 1000 > +new Date());
            console.log(`Wake up after ${time}s`);
        })
        return this
    }
    eat(things) {
        this.q.push(() => console.log("Eat", things))
        return this
    }
    sleepFirst(time) {
        this.q.unshift(() => {
            const start = +new Date();
            while (start + time * 1000 > +new Date());
            console.log(`Wake up after ${time}s`);
        })
        return this
    }
    run() {
        this.q.forEach(task => task())
    }
}
new LazyMan("Hank").sleepFirst(2).sleep(2).eat("supper");
